// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import { CWord, SHData, WordType } from "./SHData";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Node)
    layWord: cc.Node = null;

    @property(cc.Label)
    faileText: cc.Label = null;

    @property(cc.Label)
    tips: cc.Label = null;

    private btnData1: CWord = null;
    private btnData2: CWord = null;
    private btnData3: CWord = null;

    private tagData: CWord = null;

    onLoad() {
        this.ResetBtnWord();
        let children = this.layWord.children;
        for (let i = 0; i < children.length; i++) {
            children[i].on("click", () => {
                let btnData: CWord = this[`btnData${i + 1}`] as CWord;
                if (btnData.key === this.tagData.key) {
                    this.ResetBtnWord();
                    this.faileText.string = SHData.GetSuccessText();
                } else {
                    this.faileText.string = SHData.GetFaileText(this.tagData, btnData);
                }
            });
        }
    }

    private GetWords(): Array<CWord> {
        let tempArr = new Array<CWord>();
        tempArr.push(SHData.GetTiveWord(WordType.Positive));
        let tagData = SHData.GetTiveWord(WordType.Negative);
        this.tagData = tagData;
        this.tips.string = `消除目标是【${tagData.name}】`;
        tempArr.push(tagData);
        tempArr.push(SHData.GetTiveWord(WordType.Positive));
        return SHData.GetRandomArr(tempArr);
    }

    private ResetBtnWord() {
        let words: Array<CWord> = this.GetWords();
        let children = this.layWord.children;
        for (let i = 0; i < words.length; i++) {
            this[`btnData${i + 1}`] = words[i];
            this.SetBtnText(children[i], words[i]);
        }
    }

    private SetBtnText(btnNode: cc.Node, word: CWord) {
        let wordText = cc.find("Background/word", btnNode).getComponent(cc.Label);
        wordText.string = word.name;
    }

}
